package com.momo.oauth.config.handler;

import com.momo.oauth.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Kagome on 2019/5/10.
 */
@Slf4j
@Component
public class CustomLoginFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //用户登录时身份认证未通过
        if (e instanceof BadCredentialsException){
            log.info("用户登录时：用户名或者密码错误.");
            ResultUtil.writeJavaScript(httpServletResponse,"用户登录时：用户名或者密码错误");
        }else{
            ResultUtil.writeJavaScript(httpServletResponse,"用户登录时：用户名或者密码错误");
        }
    }
}